<template>
	<view class="wrap">
		<view class="topBc"></view>
		<view class="content formWrap">
			<view class="title" style="margin-top: 0;">
				<text>从业人数（其中）</text>
				<text class="hint">此项无选择默认不公示</text>
			</view>
			
			<u-form :model="formData" ref="uForm" :rules="rules">
				<u-form-item v-if="formKeys.empnum" label="从业人数" prop="empnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.empnum" placeholder="请输入" border="none" inputAlign="right">
						<view 
							slot="prefix" style="transform: translateX(-12rpx);"
							@click="$util.showModal({content:'在报告年度年末，在企业(机构) 及其分支机构工作并取得劳动报酬或者经营收入的全部人员数量，包含劳务派遣人员和临时工。'})"
						>
							<u-icon name="question-circle" :color="bgColor" size="34rpx"></u-icon>
						</view>
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.empnumdis" label="是否公示" prop="empnumdis" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.empnumdis" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item v-if="formKeys.womempnum" label="其中女性从业人数" prop="womempnum" required label-width="auto">
					<u-input v-model="formData.womempnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.womempnumdis" label="是否公示" prop="womempnumdis" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.womempnumdis" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item v-if="formKeys.memnum" label="成员人数" prop="memnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.memnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.farnum" label="其中农民人数" prop="farnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.farnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.annnewmemnum" label="本年度新增" prop="annnewmemnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.annnewmemnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.annredmemnum" label="退出" prop="annredmemnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.annredmemnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.colgranum" label="其中高校毕业生人数（经营者）" prop="colgranum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.colgranum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.retsolnum" label="其中退役士兵人数（经营者）" prop="retsolnum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.retsolnum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.dispernum" label="其中残疾人人数（经营者）" prop="dispernum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.dispernum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.unenum" label="其中失业人员再就业人数（经营者）" prop="unenum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.unenum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.colemplnum" label="其中高校毕业生人数（雇工）" prop="colemplnum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.colemplnum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.retemplnum" label="其中退役士兵人数（雇工）" prop="retemplnum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.retemplnum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.disemplnum" label="其中残疾人人数（雇工）" prop="disemplnum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.disemplnum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.uneemplnum" label="其中失业人员再就业人数（雇工）" prop="uneemplnum" type="number" required label-width="auto" borderBottom>
					<u-input v-model="formData.uneemplnum" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
			</u-form>
			
		</view>
		<view class="submitBtn_bc"></view>
		<view class="submitBtn_ubtn">
			<u-button :disabled="!$util.submitState(year,state)" @click="submit()">提交</u-button>
		</view>
	</view>
</template>

<script>
export default {
	data(){
		return{
			minFormName: 'empnum', //小表单类型名
			regnoInfo: this.$store.state.regnoInfo,
			year: '',
			state: '',
			bgColor: this.$config.bgColor,
			
			stateRanges: [
				{text: '是',value: '1'},
				{text: '否',value: '2'}
			],
			empnum: '',
			formData: {
				empnum: '',
				empnumdis: '',
				womempnum: '',
				womempnumdis: '',
				memnum: '',
				farnum: '',
				annnewmemnum: '',
				annredmemnum: '',
				
				colgranum: '',
				retsolnum: '',
				dispernum: '',
				unenum: '',
				
				colemplnum: '',
				retemplnum: '',
				disemplnum: '',
				uneemplnum: '',
			},
			rules: {},
			rulesAll: {
				empnum: this.$typeData.ruleData.peopleRule,
				empnumdis: this.$typeData.ruleData.disRule,
				womempnum: this.$typeData.ruleData.peopleRule,
				womempnumdis: this.$typeData.ruleData.disRule,
				memnum: this.$typeData.ruleData.peopleRule,
				farnum: this.$typeData.ruleData.peopleRule,
				annnewmemnum: this.$typeData.ruleData.peopleRule,
				annredmemnum: this.$typeData.ruleData.peopleRule,
				colgranum: this.$typeData.ruleData.peopleRule,
				retsolnum: this.$typeData.ruleData.peopleRule,
				dispernum: this.$typeData.ruleData.peopleRule,
				unenum: this.$typeData.ruleData.peopleRule,
				colemplnum: this.$typeData.ruleData.peopleRule,
				retemplnum: this.$typeData.ruleData.peopleRule,
				disemplnum: this.$typeData.ruleData.peopleRule,
				uneemplnum: this.$typeData.ruleData.peopleRule,
			},
			formKeys: {},
			
			peopleForm: [
				'womempnum','memnum','farnum','annnewmemnum','annredmemnum','colgranum','retsolnum',
				'dispernum','unenum','colemplnum','retemplnum','disemplnum','uneemplnum'
			]
			
		}
	},
	onLoad(option) {
		this.year = option.year;
		this.state = option.state;
		this.empnum = option.empnum;
		this.getFormKey();
		this.getFormData();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules)
	},
	methods:{
		// 判断人数总和是否符合规定
		peopleNumSet(){
			let sta = true;
			if(this.formKeys.empnum){
				let arr = Object.keys(this.formKeys);
				let allNum = 0;
				arr.forEach(item=>{
					if(this.peopleForm.includes(item) && this.formKeys[item]){
						allNum += Number(this.formData[item]);
					}
				})
				console.log(allNum,this.formData.empnum)
				if(allNum>this.formData.empnum){
					this.$util.showToast({title: '人数总和不能大于总从业人数！'})
					sta = false;
				}
			}
			return sta;
		},
		// 获取小表单数据
		getFormData(){
			let url = this.$typeData.minFormUrl.get(this.minFormName);
			if(!url){
				uni.showModal({
					title: '暂无接口'
				})
				return;
			}
			this.$http.sendRequest({
				url: url,
				data: {
					regno: this.regnoInfo.regno,
					ancheyear: this.year
				}
			})
			.then(res=>{
				console.log('接口返回',res.data);
				let data = res.data;				let keys = Object.keys(this.formKeys);				keys.forEach(item=>{					this.formData[item] = data[item];				})
				console.log(789789,this.formData);
			})
			.catch(err=>{})
		},
		//获取小表单拥有字段
		getFormKey(){
			let formKeys = this.$util.getFormKeys(this.minFormName,this.regnoInfo.enttype);
			this.formKeys = formKeys;
			let keys = Object.keys(formKeys);
			keys.forEach(item=>{
				this.rules[item] = this.rulesAll[item];
			})
		},
		submit(){
			let _this = this;
			this.$refs.uForm.validate().then(res => {
				let sta = _this.peopleNumSet();
				if(sta){
					_this.saveBtn();
				}
			}).catch(errors => {
				this.$util.showToast({title:'请完善信息'})
			})
		},
		saveBtn(){
			let sta = this.verdictNumFun();
			if(sta){
				this.$util.showToast({title: sta})
				return;
			}
			let data = {
				regno: this.regnoInfo.regno,
				ancheyear: this.year,
			}
			data = this.$util.setSaveFormData(this.formKeys,data,this.formData);
			this.$http.sendRequest({
				url: '/annual/report/baseinfo/update-empnum',
				method: 'PUT',
				data: data
			})
			.then(res=>{
				this.setRecord();
			})
			.catch(err=>{})
		},
		verdictNumFun(){
			let keys = Object.keys(this.formData);
			let num = 0;
			let sta = false;
			for(let i=0; i<keys.length; i++){
				let item = Number(this.formData[keys[i]]);
				if( item==='' || item===undefined || item===NaN){
					sta = '请输入正确的数字格式';
					return sta;
					break;
				}else{
					num += item;
				}
			}
			if(this.empnum !== undefined ||this.empnum !== '' ||this.empnum !== null){
				if(num > this.empnum){
					sta = '数量总和不应大于基本信息中的从业人数';
				}
			}
			return sta;
		},
		
		
		
		
		
		//存一下记录
		setRecord(){
			let fillingItem = this.$minFormType.get(this.minFormName);
			this.$http.sendRequest({
				url: '/annual/report/record',
				method: 'POST',
				data: {
					regno: this.regnoInfo.regno,
					year: this.year,
					fillingItem: fillingItem,
					isImp: '0',
				},
				hideLoad: true,
			})
			.then(res=>{
				uni.navigateBack();
			})
			.catch(err=>{})
		},
		goPage(path){
			uni.navigateTo({
				url: path
			})
		},
	}
}
</script>

<style lang="less" scoped>
.wrap{
	width: 100vw;
	min-height: 100vh;
	padding-top: 16rpx;
	box-sizing: border-box;
	position: relative;
	background-color: #FFF;
	
	.topBc{
		height: 10rpx;
		background-color: #F5F6F8;
	}
}

.content{
	padding: 30rpx 36rpx;
	background-color: #FFF;
	
	.title{
		color: #E4B269;
		font-size: 36rpx;
		font-weight: bold;
		border-left: 8rpx solid #E4B269;
		line-height: 40rpx;
		padding-left: 16rpx;
		margin-bottom: 30rpx;
		margin-top: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		
		.hint{
			font-size: 32rpx;
			color: #b8b8b8;
			font-weight: 400;
		}
	}
	
}
</style>